<?php

/**
 * This is the model class for table "relais".
 *
 * The followings are the available columns in table 'relais':
 * @property integer $id
 * @property integer $zoneid
 * @property integer $wiringpiPin
 * @property string $alternance
 */
class Relai extends CActiveRecord {


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Relai the static model class
     */
    public static function model($className = __CLASS__) {
	  return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	  return 'chauffage_relais';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	  // NOTE: you should only define rules for those attributes that
	  // will receive user inputs.
	  return array(
		array('zoneid, wiringpiPin', 'numerical', 'integerOnly' => true),
		array('alternance', 'length', 'max' => 3),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, zoneid, wiringpiPin, alternance', 'safe', 'on' => 'search'),
	  );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	  // NOTE: you may need to adjust the relation name and the related
	  // class name for the relations automatically generated below.
	  return array(
	  );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	  return array(
		'id' => 'ID',
		'zoneid' => 'Zoneid',
		'wiringpiPin' => 'Wiringpi Pin',
		'alternance' => 'Alternance',
	  );
    }

    /**
     * Activation/désativation du relai demandé
     * 
     * @param int $etat  etat dans lequel passer le relai
     */
    public function setEtat($etat) {
	  $setmodecmd = 'gpio mode ' . $this->wiringpiPin . ' out';
	  $setetatcmd = 'gpio write ' . $this->wiringpiPin . ' ' . $etat;

	  //echo ($setmodecmd . "\n");
	  //echo ($setetatcmd . "\n");

	  exec($setmodecmd);
	  exec($setetatcmd);
    }

}